package com.ruoyi.project.cms.pro.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.cms.notice.domain.NoticeType;
import com.ruoyi.project.cms.pro.domain.ProFile;
import com.ruoyi.project.cms.pro.service.CmsImgService;
import com.ruoyi.project.cms.pro.service.ProFileService;
import com.ruoyi.project.monitor.job.domain.Job;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.FileInfo;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * com.ruoyi.project.cms.pro.controller
 * 作者：lh
 * 描述:用于文件的添加、修改、是否是标杆文件、删除
 * 时间:2020-05-21
 */
@Controller
@RequestMapping("/pro/file")
public class ProFileController extends BaseController {

    private String prefix = "cms/pro";

    @Autowired
    private ProFileService proFileService;

    @Autowired
    private CmsImgService cmsImgService;

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 文件及数据上传
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated ProFile file)
    {
        file.setProIsBench(1);
        file.setProImgId(1);

        return toAjax(proFileService.insert(file));
    }


    /**
     * 修改文件
     */
    @GetMapping("/edit/{proId}")
    public String edit(@PathVariable("proId") Integer proId, ModelMap mmap)
    {

        mmap.put("proFile",proFileService.selectByPrimaryKey(proId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @Log(title = "公告类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ProFile file)
    {
        file.setProImgId(1);
        return toAjax(proFileService.updateByPrimaryKey(file));
    }

    /**
     * 删除公告类型
     * @param ids
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(proFileService.deleteByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 任务调度状态修改
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ProFile file) throws SchedulerException
    {

        ProFile p = proFileService.selectByPrimaryKey(file.getProId());
        System.out.println("1111111111111111111111111111111111"+file.getProIsBench());
        p.setProIsBench(file.getProIsBench());
        return toAjax(proFileService.updateByPrimaryKey(p));
    }

    /**
     * 文件名校验
     * @param file
     * @return
     */
    @PostMapping("/checkFileNameUnique")
    @ResponseBody
    public String checkFileNameUnique(ProFile file)
    {
        return proFileService.checkFileNameUnique(file);
    }




}
